﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace ACNetMusicClient
{
    public partial class Sharing : Form
    {
        private List<string> files;

        private ArrayList songsList;
        private SongController songsCtrl;

        public Sharing()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All files|*.*|Audio files|*.mp3|Wma files|*.wma|Wav files|*.wav";
            dlg.Multiselect = true;
            dlg.Title = "Select files to share";
            
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                files = dlg.FileNames.ToList();
            }

            foreach (string file in files)
            {
                listView.Items.Add(file);
            }

        }

        private void btnShare_Click(object sender, EventArgs e)
        {
            // do work in here
            foreach (ListViewItem item in listView.Items)
            {
                if (item.Checked == true)
                {
                    SongDTO song = new SongDTO();
                    FileInfo info = new FileInfo(item.Text);
                    song.Id = DateTime.Now.ToString() + DateTime.Now.Millisecond.ToString();
                    song.Name = info.Name;
                    song.Size = info.Length;
                    song.Path = info.FullName;
                    songsList.Add((object)song);
                }
            }

            // insert to database
            if (songsList.Count > 0)
            {
                songsCtrl.SongsList = songsList;
                songsCtrl.insert();

                this.Close();
            }
            else
                MessageBox.Show("No file selected!", "Warning");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void masterCheckbox_CheckStateChanged(object sender, EventArgs e)
        {
            if (masterCheckbox.Checked)
            {
                foreach (ListViewItem item in listView.Items)
                {
                    item.Checked = true;
                }
            } 
            else
            {
                foreach (ListViewItem item in listView.Items)
                {
                    item.Checked = false;
                }
            }
        }

        private void Sharing_Load(object sender, EventArgs e)
        {
            songsCtrl = new SongController();
            songsList = new ArrayList();
        }
    }
}
